package com.mycode.view;

import com.mycode.model.Account;

import javax.swing.*;
import java.awt.*;

/**
 * @author shkstart
 * @create 2021-01-22 12:28
 */
public class RegAccountDialog extends JDialog {
    //标签
    private JLabel label[];
    //文本框
    private JTextField jtfs[];
    //按钮
    private JButton jButton1;
    private JButton jButton2;
    //主窗口
    private BankEmpOper frame;

    public RegAccountDialog(BankEmpOper frame) {
        //设定关于主窗口模态
        super(frame, true);
        //设定窗口标题
        this.setTitle("开户");
        //设定大小
        this.setSize(300, 240);
        //设定关于父窗口frame对象居中
        this.setLocationRelativeTo(frame);
        //设定固定大小
        this.setResizable(false);
        //设定对话空布局
        this.setLayout(null);
        //添加组件
        addComponent();
        this.setVisible(true);

    }

    /**
     * 添加组件
     */
    private void addComponent() {
        //设定小容器面板
        JPanel jPanel = new JPanel();
        //定义布局管理器：网格
        GridLayout gridLayout = new GridLayout(4, 2, 5, 10);
        //设定jpanel为网格布局管理
        jPanel.setLayout(gridLayout);
        //构造标签
        String[] s1 = {"开户姓名", "开户金额", "开户密码", "确认密码"};
        label = new JLabel[s1.length];
        for (int i = 0; i < s1.length; i++) {
            label[i] = new JLabel(s1[i]);
            //设定标签文本对齐方式：居中
            label[i].setHorizontalAlignment(JLabel.CENTER);

        }
        //构造文本框
        jtfs = new JTextField[label.length];
        for (int i = 0; i < jtfs.length; i++) {
            if (i < 2) {
                jtfs[i] = new JTextField();
            } else {
                jtfs[i] = new JPasswordField();
            }

        }
        //添加标签和文本框到面板
        for (int i = 0; i < label.length; i++) {

            jPanel.add(label[i]);
            jPanel.add(jtfs[i]);
        }
        //设定面板的边线
        jPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), "注册信息"));
        //将面板添加到窗口
        jPanel.setBounds(5, 5, 285, 150);
        this.add(jPanel);
        //构造按钮面板
        JPanel btpanel = new JPanel();
        //构造按钮
        jButton1 = new JButton("开户");
        jButton2 = new JButton("取消");
        //添加按钮到面板
        btpanel.add(jButton1);
        btpanel.add(jButton2);
        //添加按钮面板到窗口
        btpanel.setBounds(0, 170, 300, 50);
        this.add(btpanel);

        //添加监听器
//        RegAccountDialog r1=new RegAccountDialog(this);
//        jButton1.addActionListener(r1);
//        jButton2.addActionListener(r1);


    }

    /*
     *
     * 读取所有文本框信息到对象
     *
     * */
    public Account getAcc() {
        String name = this.jtfs[0].getText();
        //校验
        if (name.trim().isEmpty()) {
            JOptionPane.showMessageDialog(this, "请正确输入开户姓名");
            return null;
        }
        String monerStr = this.jtfs[1].getText();
        if (!monerStr.matches("[1-9]\\d*")) {
            JOptionPane.showMessageDialog(this, "请正确输入开户金额", "错误", JOptionPane.ERROR_MESSAGE);

            return null;

        }
        double money = Double.parseDouble(monerStr);
        JPasswordField jPasswordField = (JPasswordField) this.jtfs[2];
        String password = new String(jPasswordField.getPassword());
        jPasswordField = (JPasswordField) this.jtfs[3];
        String password2 = new String(jPasswordField.getPassword());
        //校验
        if (password.isEmpty() || password2.isEmpty() || !password.equals(password2)) {
            JOptionPane.showMessageDialog(this, "您输入的密码不匹配", "错误", JOptionPane.ERROR_MESSAGE);
            this.jtfs[2].setText("");
            this.jtfs[3].setText("");
            return null;
        }
        Account account = new Account(name, money, password);
        return account;

    }


}
